import {useEffect,useState } from 'react';
import { preinitModule } from 'react-dom';
//模拟一个接口请求函数
async function queryData(){
  const data=await new Promise((resolve)=>{
    setTimeout(()=>{
      resolve(666)
    },2000)
  })
  return data
}
function App() {
 let [num,setNum]=useState(1)//[1,function]
//  useEffect(()=>{
//   queryData().then(data=>{
//     setNum(data)
//     console.log('Effect')
//   })
//  },[1]) 
useEffect(()=>{
  console.log('Effect')
  let timer=setInterval(()=>{
   console.log(num)
  },1000)
  return()=>{
    console.log('清除定时器')
    clearInterval(timer)
  }
},[num])

const [age,setAge]=useState(18)
 function handle(){
  //num++//num改了但是无法触发视图更新
  setNum(num+1) //将num修改为参数值，并触发视图更新
  console.log(num)
 }

 return (

   <div>
    <button onClick={handle}>{num}</button>
    <h1 onClick={()=>{setAge(age+1)}}>{age}</h1>
   </div>
 )
}

export default App;